package com.darwin.darwinIsometric.utils
{
	import com.darwin.darwinIsometric.core.Point3D;
	
	import flash.geom.Point;
	import flash.geom.Vector3D;

	/**
	 * 世界/舞台 坐标转换工具 
	 * @author vanCopper
	 * 
	 */	
	public class IsoUtils
	{
		// 1.2247...
		public static const Y_CORRECT:Number = Math.cos(-Math.PI / 6) * Math.SQRT2;
		
		/**
		 * 世界坐标转换为屏幕坐标 
		 * @param pos
		 * @return 
		 * 
		 */		
		public static function isoToScreen(pos:Point3D):Point
		{
			var screenX:Number = pos.x - pos.z;
			var screenY:Number = pos.y * Y_CORRECT + (pos.x + pos.z) * .5;
			return new Point(screenX, screenY);
		}
		
		/**
		 *  屏幕坐标转换为世界坐标
		 * @param point
		 * @return 
		 * 
		 */		
		public static function screenToIso(point:Point):Point3D
		{
			var xpos:Number = point.y + point.x * .5;
			var ypos:Number = 0;
			var zpos:Number = point.y - point.x * .5;
			return new Point3D(xpos, ypos, zpos);
		}
		
		/**
		 * 获得转向目标点的方向
		 * @param current 当前所在点
		 * @param target 目标点
		 */
		public static function targetDirection(current:Point3D,target:Point3D):int
		{
			var dx:Number = target.x - current.x;
			var dy:Number = target.z - current.z;
			var direction:Number = Math.atan2(dy,dx) * 180 / Math.PI;
			direction = direction-90;
			direction = (360 + direction)%360;
			return IsoUtils.toDirection(direction);
		}
		
		/**
		 * 转为45的倍数
		 */
		public static function toDirection(value:int):int
		{
			return Math.round(value/45) * 45;
		}
	}
}